<template>
	<view class="normal-login-container">
		<view class="logo-fixed">
			<image :src="globalConfig.appInfo.logo" mode="widthFix"></image>
		</view>
		<view class="logo-content">
			<image src="../static/images/turk-logo.png" mode="widthFix"></image>
			<view class="title">
				<text>Login</text>
			</view>
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="Account"
					placeholder-style="color:#ff9393" placeholder-class="holder-color" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="Password"
					placeholder-style="color:#ff9393" placeholder-class="holder-color" maxlength="20" />
				<view class="forgot">Forgot?</view>
			</view>
			<view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
				<view class="iconfont icon-code icon"></view>
				<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码"
					placeholder-class="holder-color" maxlength="4" />
				<view class="login-code">
					<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
				</view>
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block lg round">LOGIN</button>
			</view>
		</view>
		<view class="sign-up">
			<text class="sign-up-text">Don't have an account yet?</text>
			<view class="sign-up-btn" @click="handleToRegister">Sign up</view>
		</view>
		<view class="xieyi text-center" v-if="false">
			<text class="text-grey1">登录即代表同意</text>
			<text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
			<text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
		</view>
	</view>
</template>

<script setup>
	import { getCodeImg } from '@/api/login'
	import { ref } from "vue";
	import { onLoad,onUnload } from '@dcloudio/uni-app';
	import user from '@/store/modules/user'
	import config from '@/config.js'
	import store from '@/store'
	const codeUrl = ref("");
	const captchaEnabled = ref(false);
	const globalConfig = ref(config);
	const loginForm = ref({
		username: "admin",
		password: "1234567",
		code: "",
		uuid: ''
	});
	
	
	
	
	onLoad(()=>{
		// #ifdef APP-PLUS
		 plus.screen.lockOrientation('portrait-primary'); 
		// #endif
		uni.onWindowResize(windowResizeCallback)
	})
	onUnload(()=>{
			// #ifdef APP-PLUS
		 plus.screen.lockOrientation('portrait-primary'); 
		 	// #endif
	})
	
	
	// 隐私协议
	function handlePrivacy() {
		let site = globalConfig.value.appInfo.agreements[0];
		uni.navigateTo({
			url: `/pages/common/webview/index?title=${site.title}&url=${site.url}`
		});
	};
	// 用户协议
	function handleUserAgrement() {
		let site = globalConfig.value.appInfo.agreements[1]
		uni.navigateTo({
			url: `/pages/common/webview/index?title=${site.title}&url=${site.url}`
		});
	};

	// 获取图形验证码
	function getCode() {
		getCodeImg().then(res => {
			captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
			if (captchaEnabled.value) {
				codeUrl.value = 'data:image/gif;base64,' + res.img
				loginForm.value.uuid = res.uuid
			}
		})
	};

	async function handleLogin() {
		if (loginForm.value.username === "") {
			uni.showToast({
				title: '请输入您的账号',
				mask: false,
				duration: 1000
			});
			//this.$modal.msgError("请输入您的账号")
		} else if (loginForm.value.password === "") {
			uni.showToast({
				title: '请输入您的密码',
				mask: false,
				duration: 1000
			});
			//this.$modal.msgError("请输入您的密码")
		} else if (loginForm.value.code === "" && captchaEnabled.value) {
			uni.showToast({
				title: '请输入验证码',
				mask: false,
				duration: 1000
			});
			//this.$modal.msgError("请输入验证码")
		} else {
			uni.showLoading({
				duration: 0
			})
			//this.$modal.loading("登录中，请耐心等待...")
			pwdLogin()
		}
	};
	// 密码登录
	async function pwdLogin() {
		/* this.$modal.closeLoading() */
		setTimeout(()=>{
			uni.hideLoading();
			loginSuccess()
		},500)
		// store.dispatch('Login', loginForm.value).then(() => {
		// 	uni.hideLoading();
		// 	loginSuccess()
		// }).catch(() => {
		// 	if (captchaEnabled.value) {
		// 		getCode()
		// 	}
		// })
	};

	function loginSuccess(result) {
		// 设置用户信息
		uni.reLaunch({
			url: '/pages/index/index'
		});
		// store.dispatch('GetInfo').then(res => {
		// 	uni.reLaunch({
		// 		url: '/pages/index/index'
		// 	});
		// })
	}

	function handleToRegister() {
		uni.navigateTo({
			url: '/pages/register'
		});
	};

	if (captchaEnabled.value) {
		getCode();
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	
	.logo-fixed{
		position: fixed;
		top: 100rpx;
		left: 50rpx;
		width: 100rpx;
		image{
			width: 100%;
		}
	}

	.normal-login-container {
		width: 100%;
		height: 100%;
		position: absolute;
		overflow: hidden;
		background-image: linear-gradient(to left bottom, #F53143, #CE0E20);

		.logo-content {
			width: 255rpx;
			text-align: center;
			margin: 191rpx auto 0;

			image {
				width: 100%;
				height: 100%;
			}

			.title {
				color: #ffffff;
				font-size: 57.83rpx;
				margin-top: 48rpx;
				font-weight: bold;
			}
		}
	}

	.login-form-content {
		text-align: center;
		margin: 125rpx auto 0;
		width: 80%;
		overflow: hidden;

		.input-item {
			margin: 40rpx auto;
			height: 110rpx;
			border-bottom: 1rpx solid #FB3547;

			.icon {
				font-size: 52rpx;
				margin-left: 0;
				color: #fff;
			}

			.input {
				width: 100%;
				font-size: 32rpx;
				text-align: left;
				padding-left: 30rpx;
				color: #ffffff;

				&:input-placeholder {
					color: #fff;
				}

				&::placeholder {
					color: #fff;
				}
			}

			.holder-color {
				color: #fff;
			}

			.forgot {
				color: #F08D96;
				padding-left: 15rpx;
			}

			.login-btn {
				margin-top: 118rpx;
				height: 84rpx;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}

	.sign-up {
		position: fixed;
		bottom: 93rpx;
		left: 82rpx;

		.sign-up-text {
			font-size: 28rpx;
			color: #EB989F;
		}

		.sign-up-btn {
			color: #fff;
			font-size: 32rpx;
			font-weight: bold;
			margin-top: 20rpx;
		}
	}
</style>